﻿Public Class ViewStock
   
    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New Stock
        
        frm.l.Text = row.Cells("StockId").Value
        frm.pro = row.Cells("ProductId").Value
        frm.store = row.Cells("StoreId").Value
        frm.txtqty.Text = row.Cells("Qty").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub ViewStock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loaddata()
    End Sub
    Private Sub loaddata()
        cmd.Parameters("@S") = txtsearch.Text
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT StockId, s.ProductId,p.ProductName,p.Price,s.StoreId,st.StoreName,Qty FROM Stock s INNER JOIN Product p on p.ProductId=s.ProductId INNER JOIN Store st on st.StoreId=s.StoreId WHERE s.Active=1 AND UPPER(p.ProductName+st.StoreName) LIKE UPPER('%'+@S+'%')")
    End Sub

    Private Sub btnnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnew.Click
        Dim frm = New Stock
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("StockId").Value

        cmd.Parameters("@Id") = id
        cmd.ExecuteNonQuery("UPDATE Stock SET Active=0 WHERE StockId=@Id")
        loaddata()
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        loaddata()
    End Sub
End Class